﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class SysManager_UserDtl :BaseDataPage
{
    string uid;
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        this.Load += new System.EventHandler(Page_Load);
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        uid = Request.Params["UserId"];
        string aciton = Request.Params["aciton"];
        switch (aciton)
        {
            case "CheckUserName":
                CheckUserName();
                break;
        }

        if (string.IsNullOrEmpty(uid))
        {
            IsAdd = true;
        }
        if (!IsPostBack)
        {
            GetUserinfo(uid);
        }
      
    }

    private void CheckUserName()
    {
        string username = Request.Params["username"];
        DataSet ds = mydb.DM_GetDateSet("select * from sys_loginuser where username = '" + username + "'");
        if (ds.Tables[0].Rows.Count > 0)
        {
            Response.Write("OK");
            Response.End();
        }
        else
        {
            Response.Write("NO_DATA");
            Response.End();
        }
    }

    private void GetUserinfo(string uid)
    {
        if (!string.IsNullOrEmpty(uid))
        {
            DataSet ds = mydb.DM_GetDateSet("select * from sys_loginuser where userid = "+uid);
            if (ds.Tables[0].Rows.Count > 0)
            {
                DataRow dr = ds.Tables[0].Rows[0];
                this.txtusercode.Text = dr["usercode"].ToString();
                this.txtusername.Text = dr["username"].ToString();
                this.txtpwd.Text = dr["upassword"].ToString();
                this.txtqq.Text = dr["qq"].ToString();
                this.txtweixin.Text = dr["weixin"].ToString();
                this.txtmobile.Text = dr["mobile"].ToString();
                this.chkBlock.Checked = bool.Parse(dr["block"].ToString());
                this.chkapp.Checked = bool.Parse(dr["isApp"].ToString());
                this.apptype.SelectedValue = dr["apptype"].ToString();
            }
        }
        else
        {
            this.txtusercode.Text = "";
            this.txtusername.Text = "";
            this.txtpwd.Text = "";
            this.txtqq.Text = "";
            this.txtmobile.Text = "";
            this.txtweixin.Text = "";
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        //保存
        if (this.txtusercode.Text == "")
        {
            Response.Write("<script type='text/javascript'>alert('用户编号不能为空！')</script>");
            return;
        }
        if (this.txtusername.Text == "")
        {
            Response.Write("<script type='text/javascript'>alert('用户名称不能为空！')</script>");
            return;
        }
        if (this.txtpwd.Text == "")
        {
            Response.Write("<script type='text/javascript'>alert('密码不能为空！')</script>");
            return;
        }
        string sql = "";
        string pwd = SDKSecurity.DESEncrypt(this.txtpwd.Text, "km141201");
        if (IsAdd)
        {
            if (mydb.DM_GetDateSet("select * from sys_loginuser where username = '" + txtusername.Text+"'").Tables[0].Rows.Count > 0)
            {
                Response.Write("<script type='text/javascript'>alert('用户重复！')</script>");
                return;
            }
            sql = @"insert into sys_loginuser(usercode,username,upassword,isadmin,block,isapp)
                        values('" + this.txtusercode.Text + "','" + this.txtusername.Text + "','" + pwd + "',0,1,0)";
        }
        else
        {
            sql = @"update sys_loginuser set usercode ='" + txtusercode.Text + @"',
                                                username ='" + txtusername.Text + @"',
                                                upassword='" + pwd + @"',
                                                qq='" + this.txtqq.Text + @"',
                                                weixin='" + this.txtweixin.Text + @"',
                                                mobile='" + this.txtmobile.Text + @"',
                                                isapp=" + (this.chkapp.Checked ? "1":"0") + @",
                                                apptype=" + this.apptype.SelectedValue + @",
                                                modifydate='" + DateTime.Now + @"' 
                                               where userid=" +uid;
        }
        if (mydb.DM_ExecuteNonQuery(sql) != -1)
        {
            Response.Redirect("./UserList.aspx");
        }
        else
        {
            Response.Write("<script type='text/javascript'>alert('数据保存失败！')</script>");
            return;
        }
    }
}